{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python绘制预算开销雷达图"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "***雷达图（Radar）：***  \n",
    "* 又称为蜘蛛图、极地图、星图\n",
    "* 从同一点开始的轴上表示的两个或更多个变量的二维图表的形式显示**多变量数据**的图形方法。\n",
    "\n",
    "雷达图主要应用于企业经营状况——收益性、生产性、流动性、安全性和成长性的评价。\n",
    "\n",
    "***缺点：***  \n",
    "如果多边形过多，就会显得非常混乱，所以一般展示一到两个变量多边形"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Radar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.globals import CurrentConfig\n",
    "\n",
    "CurrentConfig.ONLINE_HOST=\"https://cdn.kesci.com/lib/pyecharts_assets/\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 数据统计结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 部门列表\n",
    "departments = ['销售', '管理', '信息技术', '客服', '研发', '市场']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 容忍的最大支出\n",
    "max_cost_threshold = [6500, 16000, 30000, 38000, 52000, 25000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 预算分配\n",
    "budget_allocation = [4300, 10000, 28000, 35000, 50000, 19000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实际开销\n",
    "actual_cost = [5000, 14000, 28000, 31000, 42000, 21000]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 绘制雷达图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置雷达图的边缘的最大值\n",
    "schema = []\n",
    "for dept, max_cost in zip(departments, max_cost_threshold):\n",
    "    schema.append(opts.RadarIndicatorItem(name=dept, max_=max_cost))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<pyecharts.options.global_options.RadarIndicatorItem at 0x1e149a82350>,\n",
       " <pyecharts.options.global_options.RadarIndicatorItem at 0x1e149a82810>,\n",
       " <pyecharts.options.global_options.RadarIndicatorItem at 0x1e149a83210>,\n",
       " <pyecharts.options.global_options.RadarIndicatorItem at 0x1e14964d110>,\n",
       " <pyecharts.options.global_options.RadarIndicatorItem at 0x1e149a90c50>,\n",
       " <pyecharts.options.global_options.RadarIndicatorItem at 0x1e149a90cd0>]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "schema"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://cdn.kesci.com/lib/pyecharts_assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"29fc647dc3c0471e8bf4b96301a726f2\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_29fc647dc3c0471e8bf4b96301a726f2 = echarts.init(\n",
       "                    document.getElementById('29fc647dc3c0471e8bf4b96301a726f2'), 'white', {renderer: 'canvas'});\n",
       "                var option_29fc647dc3c0471e8bf4b96301a726f2 = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"aria\": {\n",
       "        \"enabled\": false\n",
       "    },\n",
       "    \"color\": [\n",
       "        \"#5470c6\",\n",
       "        \"#91cc75\",\n",
       "        \"#fac858\",\n",
       "        \"#ee6666\",\n",
       "        \"#73c0de\",\n",
       "        \"#3ba272\",\n",
       "        \"#fc8452\",\n",
       "        \"#9a60b4\",\n",
       "        \"#ea7ccc\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"radar\",\n",
       "            \"name\": \"\\u9884\\u7b97\\u5206\\u914d\",\n",
       "            \"data\": [\n",
       "                [\n",
       "                    4300,\n",
       "                    10000,\n",
       "                    28000,\n",
       "                    35000,\n",
       "                    50000,\n",
       "                    19000\n",
       "                ]\n",
       "            ],\n",
       "            \"label\": {\n",
       "                \"show\": false,\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"itemStyle\": {\n",
       "                \"normal\": {}\n",
       "            },\n",
       "            \"lineStyle\": {\n",
       "                \"show\": true,\n",
       "                \"width\": 1,\n",
       "                \"opacity\": 1,\n",
       "                \"curveness\": 0,\n",
       "                \"type\": \"solid\",\n",
       "                \"color\": \"#FF0000\"\n",
       "            },\n",
       "            \"areaStyle\": {\n",
       "                \"opacity\": 0\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        },\n",
       "        {\n",
       "            \"type\": \"radar\",\n",
       "            \"name\": \"\\u5b9e\\u9645\\u5f00\\u9500\",\n",
       "            \"data\": [\n",
       "                [\n",
       "                    5000,\n",
       "                    14000,\n",
       "                    28000,\n",
       "                    31000,\n",
       "                    42000,\n",
       "                    21000\n",
       "                ]\n",
       "            ],\n",
       "            \"label\": {\n",
       "                \"show\": false,\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"itemStyle\": {\n",
       "                \"normal\": {}\n",
       "            },\n",
       "            \"lineStyle\": {\n",
       "                \"show\": true,\n",
       "                \"width\": 1,\n",
       "                \"opacity\": 1,\n",
       "                \"curveness\": 0,\n",
       "                \"type\": \"solid\",\n",
       "                \"color\": \"#0000FF\"\n",
       "            },\n",
       "            \"areaStyle\": {\n",
       "                \"opacity\": 0\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u9884\\u7b97\\u5206\\u914d\",\n",
       "                \"\\u5b9e\\u9645\\u5f00\\u9500\"\n",
       "            ],\n",
       "            \"selected\": {},\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14,\n",
       "            \"backgroundColor\": \"transparent\",\n",
       "            \"borderColor\": \"#ccc\",\n",
       "            \"borderWidth\": 1,\n",
       "            \"borderRadius\": 0,\n",
       "            \"pageButtonItemGap\": 5,\n",
       "            \"pageButtonPosition\": \"end\",\n",
       "            \"pageFormatter\": \"{current}/{total}\",\n",
       "            \"pageIconColor\": \"#2f4554\",\n",
       "            \"pageIconInactiveColor\": \"#aaa\",\n",
       "            \"pageIconSize\": 15,\n",
       "            \"animationDurationUpdate\": 800,\n",
       "            \"selector\": false,\n",
       "            \"selectorPosition\": \"auto\",\n",
       "            \"selectorItemGap\": 7,\n",
       "            \"selectorButtonGap\": 10\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"enterable\": false,\n",
       "        \"confine\": false,\n",
       "        \"appendToBody\": false,\n",
       "        \"transitionDuration\": 0.4,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5,\n",
       "        \"order\": \"seriesAsc\"\n",
       "    },\n",
       "    \"radar\": [\n",
       "        {\n",
       "            \"indicator\": [\n",
       "                {\n",
       "                    \"name\": \"\\u9500\\u552e\",\n",
       "                    \"max\": 6500\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u7ba1\\u7406\",\n",
       "                    \"max\": 16000\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4fe1\\u606f\\u6280\\u672f\",\n",
       "                    \"max\": 30000\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5ba2\\u670d\",\n",
       "                    \"max\": 38000\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u7814\\u53d1\",\n",
       "                    \"max\": 52000\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5e02\\u573a\",\n",
       "                    \"max\": 25000\n",
       "                }\n",
       "            ],\n",
       "            \"startAngle\": 90,\n",
       "            \"name\": {\n",
       "                \"textStyle\": {}\n",
       "            },\n",
       "            \"splitLine\": {\n",
       "                \"show\": true,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"splitArea\": {\n",
       "                \"show\": true,\n",
       "                \"areaStyle\": {\n",
       "                    \"opacity\": 0\n",
       "                }\n",
       "            },\n",
       "            \"axisLine\": {\n",
       "                \"show\": true,\n",
       "                \"onZero\": true,\n",
       "                \"onZeroAxisIndex\": 0\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"text\": \"\\u5404\\u90e8\\u95e8\\u9884\\u7b97\\u4e0e\\u5f00\\u9500\",\n",
       "            \"target\": \"blank\",\n",
       "            \"subtarget\": \"blank\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"textAlign\": \"auto\",\n",
       "            \"textVerticalAlign\": \"auto\",\n",
       "            \"triggerEvent\": false\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_29fc647dc3c0471e8bf4b96301a726f2.setOption(option_29fc647dc3c0471e8bf4b96301a726f2);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x1e149625b10>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "radar = (\n",
    "    Radar()\n",
    "    .add_schema(schema)\n",
    "    .add(\"预算分配\", [budget_allocation], linestyle_opts=opts.LineStyleOpts(color=\"#FF0000\"))\n",
    "    .add(\"实际开销\", [actual_cost], linestyle_opts=opts.LineStyleOpts(color=\"#0000FF\"))\n",
    "    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))\n",
    "    .set_global_opts(\n",
    "        #legend_opts=opts.LegendOpts(selected_mode=\"single\"),\n",
    "        title_opts=opts.TitleOpts(title=\"各部门预算与开销\"),\n",
    "    )\n",
    ")\n",
    "\n",
    "radar.render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
